﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace storeSMS
{
    public partial class frmMain : Form
    {
        public DataSet ds;
        public frmMain()
        {
            InitializeComponent();
        }
        private void frmMain_Load(object sender, EventArgs e)
        {
            string sql = "select top 1 com,bytl,sdays,stimes from configinfo";
            DataSet ds = DbHelperOleDb.Query(sql);
            if (ds.Tables[0].Rows.Count == 1)
            {
                myinfo.com = ds.Tables[0].Rows[0][0].ToString();
                myinfo.bytl = int.Parse(ds.Tables[0].Rows[0][1].ToString());
                myinfo.sdays=int.Parse(ds.Tables[0].Rows[0][2].ToString());
                myinfo.stimes= int.Parse(ds.Tables[0].Rows[0][3].ToString());
                //查询要发送信息列表
                DateTime dt = DateTime.Now.AddDays(myinfo.sdays);
                sql = "select ID,phone,cgddh,project,gh from storeinfo where gh<#"+dt.ToShortDateString()+"# and ID not in(select sid from sendinfos)";
                ds = DbHelperOleDb.Query(sql);
            }
            myinfo.smsinfo = ConfigurationManager.AppSettings["smsinfo"].ToString();
            
            
        }

        private void csszToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmConfig f = new frmConfig();
            //f.MdiParent = this;
            f.ShowDialog();
        }

        private void 设备参数ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmset f = new frmset();
            //f.MdiParent = this;
            f.ShowDialog();
        }

        private void fsxxToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmSend f = new frmSend();
            f.MdiParent=this;
            f.Show();

        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            frmSend f = new frmSend();
            f.MdiParent = this;
            f.Show();

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            exit();
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            exit();
            
        }

        private static void exit()
        {
            try
            {
                SMSMessage.endserveric();
            }
            catch (Exception)
            {

            }
            finally
            {
                Application.ExitThread();
            }
        }

        private void 手动发送ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmSDSend f = new frmSDSend();
            f.MdiParent = this;
            f.Show();
        }

        private void jsxxToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmReSMS f = new frmReSMS();
            f.MdiParent = this;
            f.Show();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //自动发送信息
            DateTime d = DateTime.Now;
            if (d.Hour == myinfo.stimes)
            {
                //1.找出要发送的列表
                if (ds != null && ds.Tables[0].Rows.Count > 1)
                {
                    string cgddh = "", project = "", gh = "", phone = "",sendcontent="";
                    int sid=0;
                    for (int i = 0; i < ds.Tables[0].Rows.Count;i++)
                    {
                        cgddh = ds.Tables[0].Rows[i]["cgddh"].ToString();
                        project = ds.Tables[0].Rows[i]["project"].ToString();
                        gh = ds.Tables[0].Rows[i]["gh"].ToString();
                        phone = ds.Tables[0].Rows[i]["phone"].ToString();
                        sid=int.Parse(ds.Tables[0].Rows[i]["ID"].ToString());
                        sendcontent = myinfo.smsinfo;
                        sendcontent = sendcontent.Replace("cgddh", cgddh).Replace("project", project).Replace("gh", gh);
                        SMSMessage.Send(phone,sendcontent,sid);
                    }
                }
            }
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            exit();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            jsxxToolStripMenuItem_Click(null, null);
        }

        private void 客户添加ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmLoadCustomer f = new frmLoadCustomer();
            f.MdiParent = this;
            f.Show();
        }

        private void 客户浏览ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmLoadCustomer f = new frmLoadCustomer();
            f.MdiParent = this;
            f.Show();
        }

        private void 联系人ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmlinker f = new frmlinker();
            f.MdiParent = this;
            f.Show();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            frmLoadCustomer f = new frmLoadCustomer();
            f.MdiParent = this;
            f.Show();
        }

       
    }
}
